package com.happymmall.service.impl;

import com.happymmall.dao.BaseDao;
import com.happymmall.dao.UserDao;
import com.happymmall.dao.impl.UserDaoImpl;
import com.happymmall.entity.User;
import com.happymmall.service.UserService;
import com.happymmall.util.JwtUtils;

import java.sql.Connection;
import java.sql.SQLException;

public class UserServiceImpl implements UserService {


    //业务层都会调用dao层，所以我们要引入Dao层

    // 创建一个UserDao对象，方便操作数据库
    private UserDao userDao;

    //无参构造器，一创建UserService对象就可以，创建UserDao对象
    public UserServiceImpl() {

        userDao = (UserDao) new UserDaoImpl();
    }

    @Override
    public User login(String username, String password) {
        Connection connection = null;
        User user = null;
        try {
            // 加载驱动获取链接
            connection = BaseDao.getConnection();
            // 通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(connection, password, username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;

    }

    @Override
    public int register(User user) {
        Connection connection = null;
        int result = 0;
        try {
            // 加载驱动获取链接
            connection = BaseDao.getConnection();
            // 通过业务层调用对应的具体的数据库操作

            //判断用户是否存在
            if (userDao.checkvalid(connection, user.getUsername(), "username"))
                return result;
            if (userDao.checkvalid(connection, user.getEmail(), "email"))
                return result;

            //执行插入
            result = userDao.registerUser(connection, user);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return result;

    }

    @Override
    public boolean checkvalid(String str, String type) {
        Connection connection = null;
        boolean result = false;
        try {
            // 加载驱动获取链接
            connection = BaseDao.getConnection();
            // 通过业务层调用对应的具体的数据库操作
            result = userDao.checkvalid(connection, str, type);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return result;

    }

    @Override
    public User get_user_info(int id) {
        Connection connection = null;
        User user = null;
        try {
            // 加载驱动获取链接
            connection = BaseDao.getConnection();
            // 通过业务层调用对应的具体的数据库操作
            user = userDao.get_user_info(connection, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    @Override
    public String forget_get_question(String username) {
        Connection connection = null;
        String question = null;

        try {
            connection = BaseDao.getConnection();
            question = userDao.forget_get_question(connection, username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(connection, null, null);
        }

        return question;
    }

    @Override
    public String forget_check_answer(String username, String question, String answer) {
        Connection connection = null;
        boolean iscorrect = false;
        String forgetToken = null;

        try {
            connection = BaseDao.getConnection();
            iscorrect = userDao.forget_check_answer(connection, username, question, answer);
            if (iscorrect) {
                forgetToken = JwtUtils.generateToken();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return forgetToken;

    }

    @Override
    public boolean forget_resert_password(String username, String passwordNew, String forgetToken) {
        boolean result = false;
        Connection connection = null;
        int row = 0;

        try {
            connection = BaseDao.getConnection();
            if (!JwtUtils.isValidToken(forgetToken)) {
                return result;
            }
            row = userDao.forget_reset_password(connection, passwordNew, username);
            if (row > 0) {
                return !result;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return result;


    }

    @Override
    public boolean reset_password(Integer id, String passwordOld, String passwordNew) {
        boolean result = false;
        Connection connection = null;
        int row = 0;

        try {
            connection = BaseDao.getConnection();
            if (id == null) {
                return result;
            }
            row = userDao.reset_password(connection, id, passwordOld, passwordNew);
            if (row > 0) {
                return !result;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return result;
    }

    @Override
    public boolean update_information(String email, String phone, String question, String answer, Integer id) {
        boolean result = false;
        Connection connection = null;
        int row = 0;

        try {
            connection = BaseDao.getConnection();
            if (id == null) {
                return result;
            }
            row = userDao.update_information(connection, email, phone, question, answer, id);
            if (row > 0) {
                return !result;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return result;
    }


}
